package labox.innovation.gameserver.instancemanager;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javolution.util.FastMap;
import labox.innovation.config.Config;
import labox.innovation.gameserver.model.quest.Quest;
import labox.innovation.gameserver.scripting.L2ScriptEngineManager;
import labox.innovation.gameserver.scripting.ScriptManager;

public class QuestManager extends ScriptManager<Quest> {
	
	protected static final Logger _log = LoggerFactory.getLogger(QuestManager.class.getName());

	public static final QuestManager getInstance() {
		return SingletonHolder._instance;
	}

	// =========================================================

	// =========================================================
	// Data Field
	private Map<String, Quest> _quests = new FastMap<String, Quest>();

	// =========================================================
	// Constructor
	private QuestManager() {
		_log.info("Initializing QuestManager",Config.SERVER_ID, "���������");
	}

	// =========================================================
	// Method - Public
	/**
	 * ���¼���ĳ������,ͨ��������Ʋ���
	 */
	public final boolean reload(String questFolder) {
		Quest q = getQuest(questFolder);
		if (q == null) {
			return false;
		}
		return q.reload();
	}

	/**
	 * Reloads a the quest given by questId.<BR>
	 * ���¼�������,ͨ������id<br>
	 * <B>NOTICE: Will only work if the quest name is equal the quest folder
	 * name</B>
	 * 
	 * @param questId
	 *            The id of the quest to be reloaded
	 * @return true if reload was successful, false otherwise
	 */
	public final boolean reload(int questId) {
		Quest q = this.getQuest(questId);
		if (q == null) {
			return false;
		}
		return q.reload();
	}

	/**
	 * ���¼�����������ű�
	 */
	public final void reloadAllQuests() {
		_log.info("Reloading Server Scripts",Config.SERVER_ID, "���������");
		try {
			// unload all scripts
			for (Quest quest : _quests.values()) {
				if (quest != null)
					quest.unload(false);
			}

			_quests.clear();
			// now load all scripts
			File scripts = new File(Config.DATAPACK_ROOT + "/data/scripts.cfg");
			L2ScriptEngineManager.getInstance().executeScriptList(scripts);
			QuestManager.getInstance().report();
		} catch (IOException ioe) {
			_log.error("Failed loading scripts.cfg, no script going to be loaded",Config.SERVER_ID,ioe);
		}
	}

	public final void report() {
		_log.info("Loaded: " + _quests.size() + " quests",Config.SERVER_ID, "���������");
	}

	public final void save() {
		for (Quest q : _quests.values()) {
			q.saveGlobalData();
		}
	}

	// =========================================================
	// Property - Public
	public final Quest getQuest(String name) {
		return _quests.get(name);
	}

	public final Quest getQuest(int questId) {
		for (Quest q : _quests.values()) {
			if (q.getQuestIntId() == questId)
				return q;
		}
		return null;
	}

	/**
	 * �������ģ�嵽�����������
	 */
	public final void addQuest(Quest newQuest) {
		if (newQuest == null) {
			throw new IllegalArgumentException("Quest argument cannot be null");
		}
		Quest old = _quests.get(newQuest.getName());

		// FIXME: unloading the old quest at this point is a tad too late.
		// the new quest has already initialized itself and read the data, starting
		// an unpredictable number of tasks with that data.  The old quest will now
		// save data which will never be read.
		// However, requesting the newQuest to re-read the data is not necessarily a 
		// good option, since the newQuest may have already started timers, spawned NPCs
		// or taken any other action which it might re-take by re-reading the data. 
		// the current solution properly closes the running tasks of the old quest but
		// ignores the data; perhaps the least of all evils...
		if (old != null) {
			old.unload();
			_log.info("Replaced: (" + old.getName() + ") with a new version (" + newQuest.getName() + ")",Config.SERVER_ID, "���������");

		}
		_quests.put(newQuest.getName(), newQuest);
	}

	public final boolean removeQuest(Quest q) {
		return _quests.remove(q.getName()) != null;
	}

	/**
	 * @see labox.innovation.gameserver.scripting.ScriptManager#getAllManagedScripts()
	 */
	@Override
	public Iterable<Quest> getAllManagedScripts() {
		return _quests.values();
	}

	/**
	 * @see labox.innovation.gameserver.scripting.ScriptManager#unload(labox.innovation.gameserver.scripting.ManagedScript)
	 */
	@Override
	public boolean unload(Quest ms) {
		ms.saveGlobalData();
		return this.removeQuest(ms);
	}

	/**
	 * @see labox.innovation.gameserver.scripting.ScriptManager#getScriptManagerName()
	 */
	@Override
	public String getScriptManagerName() {
		return "QuestManager";
	}

	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final QuestManager _instance = new QuestManager();
	}
}
